import 'package:environmental_les_app/common/net/net.dart';
import 'package:environmental_les_app/common/utils.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

class HomeController extends GetxController with GetTickerProviderStateMixin {
  HomeController();

  ScrollController scrollController = ScrollController();

  String location = "";

  //列表加载状态
  int listLoadStatus = 0; // 0:加载中 1:加载成功 2:加载失败 3:没有数据

  int tabClickIndex = 0; // tab 点击index

  bool loadMore = false;

  int page = 1;

  bool noMoreData = false;

  // 待办事件列表数据
  List todoList = [];

  List<String> tabs = ["报警线索", "待办任务", "巡查事件"];

  late TabController tabController;

  // 线索筛选条件
  List<Map<String, dynamic>> clueFilter = [
    {
      "title": "站点类型",
      "prop": "siteType",
      "multiSelect": false,
      "filter": [
        {"name": "生活污水污水", "value": "1"},
        {"name": "工业废水", "value": "2"},
        {"name": "雨水", "value": "3"},
      ],
    },
  ];
  // 线索筛选条件选择
  final Map<String, List<String>> clueSelectedFilters = {};

  // 线索筛选条件选择更新
  void updateClueSelection(Map<String, dynamic>? result) {
    updateSelectedFilters(clueSelectedFilters, clueFilter, result);
  }

  // 任务筛选条件
  List<Map<String, dynamic>> taskFilter = [
    {
      "title": "任务类型",
      "prop": "taskType",
      "multiSelect": false,
      "filter": [
        {"name": "日常任务", "value": "1"},
        {"name": "临时任务", "value": "2"},
      ],
    },
    {
      "title": "任务状态",
      "prop": "status",
      "multiSelect": true,
      "filter": [
        {"name": "未开始", "value": "1"},
        {"name": "进行中", "value": "2"},
        {"name": "已完成", "value": "3"},
      ],
    },
  ];
  // 任务筛选条件选择
  final Map<String, List<String>> taskSelectedFilters = {};
  // 任务筛选条件选择更新
  void updateTaskSelection(Map<String, dynamic>? result) {
    updateSelectedFilters(taskSelectedFilters, taskFilter, result);
  }

  // 事件筛选条件
  List<Map<String, dynamic>> eventFilter = [
    {
      "title": "站点类型",
      "prop": "eventType",
      "multiSelect": false,
      "filter": [
        {"name": "水质异常", "value": "1"},
        {"name": "设备故障", "value": "2"},
        {"name": "环境异常", "value": "3"},
      ],
    },
    {
      "title": "巡查状态",
      "prop": "status",
      "multiSelect": true,
      "filter": [
        {"name": "未处理", "value": "1"},
        {"name": "处理中", "value": "2"},
        {"name": "已处理", "value": "3"},
      ],
    },
  ];
  // 事件筛选条件选择
  final Map<String, List<String>> eventSelectedFilters = {};
  // 事件筛选条件选择更新
  void updateEventSelection(Map<String, dynamic>? result) {
    updateSelectedFilters(eventSelectedFilters, eventFilter, result);
  }

  _initData() {
    update(["home"]);
  }

  /// 下拉刷新
  Future onRefresh() async {
    page = 1;
    loadMore = false;
    noMoreData = false;
  }

  // 获取报警线索、待办任务、巡查事件
  void fetchData(page, pageSize, type) {
    NetUtils.post(
      ServiceApi.clueEventList,
      data: null,
      successCallback: (result) {
        todoList = result?["records"] ?? [];
        todoList = [
          {"title": "测试数据1", "time": "2024-06-01 10:00:00", "status": "未处理"},
          {"title": "测试数据2", "time": "2024-06-02 11:00:00", "status": "处理中"},
          {"title": "测试数据3", "time": "2024-06-03 12:00:00", "status": "已处理"},
          {"title": "测试数据4", "time": "2024-06-04 13:00:00", "status": "未处理"}
        ];
      },
      errorCallback: (code, msg) {
        logger.e(msg);
        todoList = [
          {"title": "测试数据1", "time": "2024-06-01 10:00:00", "status": "未处理"},
          {"title": "测试数据2", "time": "2024-06-02 11:00:00", "status": "处理中"},
          {"title": "测试数据3", "time": "2024-06-03 12:00:00", "status": "已处理"},
          {"title": "测试数据4", "time": "2024-06-04 13:00:00", "status": "未处理"}
        ];
      },
    );
  }

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: tabs.length, vsync: this);
  }

  /// tab 点击
  onTabClick(index) {
    tabClickIndex = index;
    _initData();
  }

  @override
  void onReady() {
    super.onReady();
    fetchData(1, 10, tabs[tabClickIndex]);
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
